package com.fy.fyspace.model.enums.file;

import org.apache.commons.lang3.ArrayUtils;

public enum FileCategoryEnums {
    VIDEO(1, "video", "视频", new String[]{".mp4", ".avi", ".rmvb", ".mkv", ".mov", ".ykv"}),
    MUSIC(2, "music", "音频", new String[]{".mp3", ".wav", ".wma", ".mp2", ".flac", ".midi", ".ra", ".ape", ".aac", ".cda"}),
    IMAGE(3, "image", "图片", new String[]{".jpeg", ".jpg", ".png", ".gif", ".bmp", ".dds", ".psd", ".pdt", ".webp", ".xmp", ".svg", ".tiff"}),
    DOC(4, "doc", "文档", new String[]{".pdf", ".doc", ".docx", ".xlsx", ".txt"}),
    OTHERS(5, "others", "其他", new String[]{".h", ".c", ".hpp", ".hxx", ".cpp", ".cc", ".c++", ".cxx", ".m", ".o", ".s", ".dll", ".cs",
            ".java", ".class", ".js", ".ts", ".css", ".scss", ".vue", ".jsx", ".sql", ".md", ".json", ".html", ".xml",
            ".rar", ".zip", ".7z", ".cab", ".arj", ".lzh", ".tar", ".gz", ".ace", ".uue", ".bz", ".jar", ".iso", ".mpq"});

    private Integer category;
    private String code;
    private String desc;
    private String[] suffixes;

    FileCategoryEnums(Integer category, String code, String desc, String[] suffixes) {
        this.category = category;
        this.code = code;
        this.desc = desc;
        this.suffixes = suffixes;
    }

    public static FileCategoryEnums getByCode(String code) {
        for (FileCategoryEnums item : FileCategoryEnums.values()) {
            if (item.getCode().equals(code)) {
                return item;
            }
        }
        return OTHERS;
    }

    public Integer getCategory() {
        return category;
    }

    public String getCode() {
        return code;
    }

    public String getDesc() {
        return desc;
    }

    public String[] getSuffixes() {
        return suffixes;
    }

    /**
     * 通过文件后缀获取文件分类的category
     */
    public static Integer getCategory(String suffix) {
        if (suffix == null || suffix.isEmpty()) {
            return OTHERS.getCategory();
        }
        
        suffix = suffix.toLowerCase();
        for (FileCategoryEnums item : FileCategoryEnums.values()) {
            if (ArrayUtils.contains(item.getSuffixes(), suffix)) {
                return item.getCategory();
            }
        }
        return OTHERS.getCategory();
    }

    /**
     * 通过category获取code
     */
    public static String getCodeByCategory(Integer category) {
        for (FileCategoryEnums item : FileCategoryEnums.values()) {
            if (item.getCategory().equals(category)) {
                return item.getCode();
            }
        }
        return OTHERS.getCode();
    }
}